// pages/cart/index.js
import {saveOrder} from "../../api/cart.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[],
    sellAll:true,
    total:0
  },

  //结算数据
 order: {
    "all_count": 0,
    "cancel_time": "",
    "close_type": 0,
    "delete_status": 0,
    "delivery_time": "",
    "delivery_type": 0,
    "finally_time": "",
    "is_payed": 0,
    "orderAddr": {
      "addr": "",
      "area": "",
      "area_id": 0,
      "city": "",
      "city_id": 0,
      "consignee": "",
      "lat": 0,
      "lng": 0,
      "mobile": "",
      "post_code": "",
      "province": "",
      "province_id": 0,
      "user_id": 0
    },
    "orderItems": [
      {
        "category_id": 0,
        "count": 0,
        "delivery_type": 0,
        "order_id": 0,
        "order_item_id": 0,
        "pic": "",
        "price": 0,
        "shop_cart_time": "",
        "shop_id": 0,
        "sku_id": 0,
        "sku_name": "",
        "spu_id": 0,
        "spu_name": "",
        "spu_total_amount": 0,
        "user_id": 0
      }
    ],
    "pay_time": "",
    "settled_time": "",
    "shop_id": 0,
    "shop_name": "",
    "status": 0,
    "total": 0,
    "user_id": 0,
    "version": 0
  },

  /**
   * 页面加载时刷新数据
   */
  init(){
    this.setData({
      goodsList:wx.getStorageSync('carlist')
    })
    console.log("goodsList>>>>>>>>>",this.data.goodsList)
  },
  //计算总价格
  totals(e){
    let tmp=this.data.goodsList;
    let totalPrice=0
    tmp.forEach(el=>{
      if (el.checkd) {
        totalPrice=totalPrice+parseInt(el.num)*parseInt(el.price_fee)
      }
    });
    this.setData({
      total:totalPrice
    });
  },
  //点击全选
  selectAll(e){
    //跟随全选框的状态进行改变
    let emp = !this.data.sellAll;
    console.log("emp>>>>>>>",emp);
    //循环遍历商品的复选框
    let empList=this.data.goodsList;
    empList.forEach(e=>{
      e.checkd=emp
    });
    //将数据发送到渲染页面
    this.setData({
      sellAll:emp,
      goodsList:empList
    });
    this.totals();
  },

  //商品复选框
  radioChec(el){
    //获取当前对象 
    let index=el.currentTarget.dataset.e;
    let item=this.data.goodsList[index];
    item.checkd=!item.checkd;
    this.setData({
      goodsList:this.data.goodsList
    });
    //判断商品列表中复选框是否有未false
    let flag=false;
    for(let i in this.data.goodsList){
      let itch=this.data.goodsList[i];
      if(!itch.checkd){
        flag=true;
      }
    }
    if(flag){
      this.setData({
        sellAll:false
      })
    }else{
      this.setData({
        sellAll:true
      })
    }
    this.totals();
  },
//jian
  jian(event){
    //获取当前对象 
let index=event.currentTarget.dataset.index;
let item=this.data.goodsList[index];
  if(item.num==1)
  return;
  item.num=item.num-1;
  this.setData({
    "goodsList":this.data.goodsList
  }); 
  wx.setStorageSync('carlist', this.data.goodsList)
  this.totals();
},
//add
add(event){
//获取当前对象 
let index=event.currentTarget.dataset.index;
let item=this.data.goodsList[index];
item.num=item.num+1;
this.setData({
  "goodsList":this.data.goodsList
});
wx.setStorageSync('carlist', this.data.goodsList)
this.totals();
},  
//删除选中的购物车商品      
delet(e){
    let tmoList=this.data.goodsList;
    for(let i in this.data.goodsList){
      let stat=tmoList[i].checkd;
      if (stat) {
        tmoList.splice(i,1);
      }
    }
    this.setData({
      goodsList:tmoList
    })
    wx.setStorageSync('carlist', this.data.goodsList)
    this.totals();
},

//结算
async settlement(e){

  
  let re = await saveOrder(order);
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    this.totals();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.init();

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})